package com.ys.basebindinggdemo.ui.main

import androidx.fragment.app.Fragment
import com.ys.basebindinggdemo.R
import com.ys.basebindinggdemo.databinding.FragmentMainBinding
import com.ys.basebindinggdemo.ui.base.BaseFragment
import com.ys.basebindinggdemo.ui.dashboard.DashboardFragment
import com.ys.basebindinggdemo.ui.home.HomeFragment
import com.ys.basebindinggdemo.ui.notifications.NotificationsFragment

class MainFragment : BaseFragment<FragmentMainBinding, MainViewModel>() {

    private val fragmentList =
        mutableListOf(HomeFragment(), DashboardFragment(), NotificationsFragment())

    override fun init() {
        mBinding.apply {
            navView.setOnItemSelectedListener {
                when (it.itemId) {
                    R.id.navigation_home -> replaceFragment(fragmentList[0])
                    R.id.navigation_dashboard -> replaceFragment(fragmentList[1])
                    R.id.navigation_notifications -> replaceFragment(fragmentList[2])
                    else -> replaceFragment(fragmentList[2])
                }
                true
            }


        }


        replaceFragment(fragmentList[0])
    }

    private fun replaceFragment(fragment: Fragment) {

       if (fragment.isAdded) {
            childFragmentManager.beginTransaction()
                .replace(R.id.fragment, fragment)
        } else {
            childFragmentManager.beginTransaction()
                .add(R.id.fragment, fragment)
        }.commit()
    }
}